var wineApp = wineApp || {};
wineApp.account = (function() {
        var key = wineApp.CONFIG.STORAGE.PREFIX + '.' + 'userInfo';
        var userInfo = wineApp.localStorage.get(key);
        var resourceURL = wineApp.tool.getResourceURL();
        var initAccountPage = function() {
            checkLogin();
            loadMember();
            loadMyInfo();
            loadAgentQR();
            initEvent();
            //initDrInfo();
            //initOrderStatus();
            //initMyTrackCount();
            //initMyFocus();
            wineApp.common.initBanner();
        }

        var loadMyInfo = function() {
            if (userInfo) {
                $('#username').html(userInfo.name);
                //$('#logo').attr('src', '')
            }
        }

        var checkLogin = function() {
            var loginFlag = wineApp.localStorage.get(wineApp.CONFIG.STORAGE.PREFIX + '.login_flag');
            //登录成功，需要再验证一下登录，才能调用后面的api
            if (loginFlag === "checked") {
                var username = wineApp.localStorage.get(wineApp.CONFIG.STORAGE.PREFIX + '.username');
                var password = wineApp.localStorage.get(wineApp.CONFIG.STORAGE.PREFIX + '.password');
                wineApp.biz.common.login(username, password, function(data) {
                    wineApp.biz.common.saveToStorage(true, data);
                });
            }

        }

        var initEvent = function() {
            $(document).on('click', '.signout', function() {
                wineApp.biz.common.saveToStorage(false);
                window.location.href = '../index.html';
            }).on('click', '#cash', function() {
                var ajaxUrl = wineApp.CONFIG.API.myqrurl;
                wineApp.ajax.post(ajaxUrl, {}).done(function(data) {

                });
                var isCashed = $(this).hasClass('wine_disabled');
                if (isCashed) {
                    return;
                }
            });
        }

        var loadAgentQR = function() {
            var ajaxUrl = wineApp.CONFIG.API.myqrurl;
            wineApp.ajax.post(ajaxUrl, {})
                .done(function(data) {
                    if (data.agentqrUrl) {
                        $('.twowm img').attr('src', data.agentqrUrl);
                        wineApp.tool.saveImgToLocalExt('.twowm img');
                    }
                });
        }

        var loadMember = function() {
            var ajaxUrl = wineApp.CONFIG.API.myagentinfo;
            wineApp.ajax.post(ajaxUrl, {})
                .done(function(data) {
                    var list = "<ul>";
                    if (data.up) {
                        $('#upper').html(data.up.name);
                    }
                    var unpayed = 0
                    if (data.agent) {
                        var sales = data.agent.sale;
                        var num = data.agent.down1 + data.agent.down2 + data.agent.down3;
                        var commi = data.agent.commi;
                        var payed = data.agent.payed;
                        unpayed = commi - payed;
                        $('#sales').html(sales);
                        $('#num').html(num);
                        $('#commi').html(commi);
                        $('#payed').html(payed);
                        $('#unpayed').html(unpayed);
                    }

                    if (unpayed == 0) {
                        $('#cash a').addClass('wine_disabled');
                    }
                });
        }
        return {
            checkLogin: checkLogin,
            //initPage: initPage,
            initAccountPage: initAccountPage
        }
    })()
    /*$(window).load(function(){
        console.log('before');
        wineApp.account.checkLogin();
    });
    $(document).bind('pagebeforeshow','#my-page',function(){
    });*/


$(document).ready(function() {
    console.log('after');
    wineApp.common.initHeaderIcon(0, false, {
        camera: false,
        search: true,
        more: true
    });
    wineApp.account.initAccountPage();
});